﻿package mortal.game.view.achievement.item
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.mui.controls.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.view.achievement.data.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.item.*;
    import mortal.mvc.core.*;

    public class AchievementTotalCell extends GSprite
    {
        private var _bg:GBitmap;
        private var _nameBg:GBitmap;
        private var _levelTxt:GTextFiled;
        private var _item:BaseItem;
        private var _proTxt:GTextFiled;
        private var _getBtn:GLoadedButton;
        protected var _isComFlag:GImageBitmap;
        private var _titleIconArr:Array;
        private var _info:SAchievementCategory;

        public function AchievementTotalCell()
        {
            this._titleIconArr = [];
            this._titleIconArr = ["", ImagesConst.AchievemenCategoryCellRole, ImagesConst.AchievemenCategoryCellEquip, ImagesConst.AchievemenCategoryCellRide, ImagesConst.AchievemenCategoryCellGuild, ImagesConst.AchievemenCategoryCellSociety, ImagesConst.AchievemenCategoryCellHero, "", "", ImagesConst.AchievemenCategoryCellHide];
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._bg = UIFactory.bitmap(ImagesConst.AchievementCategoryCellBg, 0, 0, this);
            this._levelTxt = UIFactory.gTextField("Lv.7", 0, 25, 132, 20, this);
            var _loc_1:* = GlobalStyle.textFormatBai;
            _loc_1.align = TextFormatAlign.CENTER;
            this._levelTxt.defaultTextFormat = _loc_1;
            this._item = UIFactory.getUICompoment(BaseItem);
            this._item.setItemStyle(ItemStyleConst.Big);
            this._item.isDragAble = false;
            UIFactory.setObjAttri(this._item, 34, 62, -1, -1, this);
            this._proTxt = UIFactory.gTextField("100/200", 0, 45, 132, 20, this);
            this._proTxt.defaultTextFormat = _loc_1;
            this._getBtn = UIFactory.gLoadedButton(ImagesConst.AchievemntGetBtn_upSkin, 43, 135, 53, 27, this);
            this._getBtn.configEventListener(MouseEvent.CLICK, this.ongetTotalRewardHd);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._info = null;
            this._bg.dispose(param1);
            this._bg = null;
            if (this._nameBg)
            {
                this._nameBg.dispose(param1);
                this._nameBg = null;
            }
            this._levelTxt.dispose(param1);
            this._levelTxt = null;
            this._item.dispose(param1);
            this._item = null;
            this._proTxt.dispose(param1);
            this._proTxt = null;
            this._getBtn.dispose(param1);
            this._getBtn = null;
            if (this._isComFlag)
            {
                this._isComFlag.dispose(param1);
                this._isComFlag = null;
            }
            return;
        }// end function

        public function updateCell(param1:SAchievementCategory) : void
        {
            this._info = param1;
            this.clear();
            if (param1 == null)
            {
                return;
            }
            var _loc_2:* = param1.category;
            if (_loc_2 >= 1 && _loc_2 < this._titleIconArr.length)
            {
                this._nameBg = UIFactory.bitmap(this._titleIconArr[_loc_2], 40, 6, this);
            }
            this._levelTxt.text = "Lv." + param1.level;
            var _loc_3:* = this.getTotal(param1);
            var _loc_4:* = param1.currentPoints > _loc_3 ? (_loc_3) : (param1.currentPoints);
            this._proTxt.text = _loc_4 + "/" + _loc_3;
            this.updateItem(param1);
            this.updateBtn(param1);
            this.showOver();
            return;
        }// end function

        private function showOver() : void
        {
            var _loc_1:* = AchievementConfig.instance.categoryRewardMaxLevelObj[this._info.category + ""];
            if (this._info.status == EAchieveStatus._EAchieveStatusOver)
            {
                if (this._isComFlag == null)
                {
                    this._isComFlag = UIFactory.gImageBitmap(ImagesConst.AchievementCompleteFlag, 20, 74, this);
                }
                this._isComFlag.visible = true;
            }
            else if (this._isComFlag)
            {
                this._isComFlag.visible = false;
            }
            return;
        }// end function

        private function clear() : void
        {
            this._levelTxt.text = "";
            this._proTxt.text = "";
            if (this._nameBg)
            {
                this._nameBg.dispose();
                this._nameBg = null;
            }
            return;
        }// end function

        public function updateByInfo(param1:SAchievementCategory) : Boolean
        {
            if (this._info && this._info.category == param1.category)
            {
                this.updateCell(param1);
                return true;
            }
            return false;
        }// end function

        private function getTotal(param1:SAchievementCategory) : int
        {
            var _loc_3:int = 0;
            var _loc_2:* = AchievementConfig.instance.categoryRewardObj[param1.category + "_" + param1.level];
            if (_loc_2)
            {
                return _loc_2.achievementPoint;
            }
            _loc_3 = uint((param1.level - 1));
            _loc_2 = AchievementConfig.instance.categoryRewardObj[param1.category + "_" + _loc_3];
            if (_loc_2)
            {
                return _loc_2.achievementPoint;
            }
            return 100;
        }// end function

        private function updateItem(param1:SAchievementCategory) : void
        {
            var _loc_4:int = 0;
            var _loc_2:* = AchievementConfig.instance.categoryRewardObj[param1.category + "_" + param1.level];
            if (_loc_2 == null)
            {
                this._item.itemData = null;
                _loc_4 = param1.level - 1;
                _loc_2 = AchievementConfig.instance.categoryRewardObj[param1.category + "_" + _loc_4];
                if (_loc_2 == null)
                {
                    return;
                }
            }
            var _loc_3:* = ItemConfig.instance.getConfig(_loc_2.rewards) as ItemGiftBagInfo;
            if (_loc_3)
            {
                this._item.itemCode = _loc_2.rewards;
            }
            return;
        }// end function

        private function updateBtn(param1:SAchievementCategory) : void
        {
            switch(param1.status)
            {
                case EAchieveStatus._EAchieveStatusCompelete:
                {
                    this._getBtn.enabled = true;
                    this._getBtn.mouseEnabled = true;
                    break;
                }
                case EAchieveStatus._EAchieveStatusNotCompelete:
                {
                    this._getBtn.enabled = false;
                    this._getBtn.mouseEnabled = false;
                    break;
                }
                default:
                {
                    this._getBtn.visible = false;
                    break;
                    break;
                }
            }
            return;
        }// end function

        private function ongetTotalRewardHd(event:MouseEvent) : void
        {
            if (this._info == null)
            {
                return;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.AchievementGetCategoryBonus, this._info.category));
            return;
        }// end function

        public function flyIcon(param1:int) : Boolean
        {
            var _loc_2:Point = null;
            if (this._info == null)
            {
                return false;
            }
            if (this._info.category != param1)
            {
                return false;
            }
            if (this._item && this._item.parent)
            {
                _loc_2 = this._item.localToGlobal(GameConst.yellowPoint);
                FlyToNavbarTool.flyBitmapOriginalToPack(this._item.bitmapdata, _loc_2);
            }
            return true;
        }// end function

    }
}
